

#define Linux011 1
#ifdef Linux011
#define   __LIBRARY__
#include <unistd.h>
#include <stdio.h>

static inline _syscall2(sem_t *,sem_open,const char *,name,unsigned int,value)
static inline _syscall1(int,sem_wait,sem_t *,sem)
static inline _syscall1(int,sem_post,sem_t *,sem)
static inline _syscall1(int,sem_unlink,const char *,name)
static inline _syscall1(int,shmget,int,key)
static inline _syscall1(int,shmat,int,shmid)

#else
#include <stdio.h>
#include <sys/stat.h> 
#include <fcntl.h>   
#include <semaphore.h> 
#include <unistd.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/types.h>
#endif

#define BUFSIZE 10
int  buf_out = 0;  

sem_t * empty,* full,* mutex;
int i,shmid,*shm_addr;

int main()
{

#ifdef Linux011
    empty = sem_open("empty",3);
    full = sem_open("full",0);
    mutex = sem_open("mutex",1);
    shmid = shmget(0x12345678);
    shm_addr = (int*)shmat(shmid);
#else
    empty = sem_open("empty",O_CREAT,0666,3);
    full = sem_open("full",O_CREAT,0666,0);
    mutex = sem_open("mutex",O_CREAT,0666,1);
    shmid = shmget(0x12345678, BUFSIZE*4, IPC_CREAT|0666);
    shm_addr = (int*)shmat(shmid, 0, 0);
#endif
 for (i = 0; i < 500; i++){
    sem_wait(full);
    sem_wait(mutex);
    printf("consumer-%d:  %d\n",i,shm_addr[buf_out]);
    shm_addr[buf_out] = 0;
    buf_out = (buf_out + 1) % BUFSIZE;
    fflush(stdout);
    sem_post(mutex);
    sem_post(empty);
 }

    sem_unlink("empty");
    sem_unlink("mutex");
    sem_unlink("full");

    return 0;
}
